package com.org.khtn.gui;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import com.google.common.collect.Multimap;

public class MyTableModel extends AbstractTableModel {

    private Vector colHeaders;
    private Vector tbData;
    

    public MyTableModel(Map<String, Multimap<String,String>> m) {
        colHeaders = new Vector();
        tbData = new Vector();

        /*
        Set setRecord = hm.entrySet();
        Iterator itRecord = setRecord.iterator();
        int i = 0;
        while (itRecord.hasNext()) {
            // key=value separator this by Map.Entry to get key and value
            Map.Entry m = (Map.Entry) itRecord.next();
            // getKey is used to get key of Map
            String number = (String) m.getKey();
            // getValue is used to get value of key in Map
            HashMap atts = (HashMap) m.getValue();
            
            if (i == 0) {
                colHeaders.add("");
            }

            Set setAtt = atts.entrySet();
            Iterator itAtt = setAtt.iterator();
            Vector dataRow = new Vector();
            dataRow.add(number);
            while (itAtt.hasNext()) {
                // key=value separator this by Map.Entry to get key and value
                Map.Entry me = (Map.Entry) itAtt.next();
                // getKey is used to get key of Map
                String att = (String) me.getKey();
                // getValue is used to get value of key in Map
                String value = (String) me.getValue();
                if (i == 0) {
                    colHeaders.add(att);
                }
                dataRow.add(value);
            }
            tbData.add(dataRow);
            i++;
        }*/
        int i = 0;
        for (Map.Entry<String, Multimap<String,String>> e : m.entrySet()) {
        	if (i == 0) {
                colHeaders.add("");
            }
        	String number = e.getKey();
        	Multimap<String,String> atts = e.getValue();
        	Vector dataRow = new Vector();
            dataRow.add(number);
        	for (Entry<String, Collection<String>> entry : atts.asMap().entrySet()) {
        		String att = (String) entry.getKey();
        		List<String> value = new ArrayList<String>();
        		value.addAll(entry.getValue());
        		if (i == 0) {
        			for(int j=0; j<value.size(); j++) {
        				colHeaders.add(att);
        				System.out.println(att);
        			}
                }
        		for (int j=0; j<value.size(); j++) {
        			dataRow.add(value.get(j));
        		}
                //dataRow.add(value);
        	}
        	tbData.add(dataRow);
            i++;
        }
    }

    @Override
    public int getColumnCount() {
        return colHeaders.size();
    }

    @Override
    public int getRowCount() {
        return tbData.size();
    }

    @Override
    public String getColumnName(int col) {
        return colHeaders.get(col).toString();
    }

    @Override
    public Object getValueAt(int row,
            int column) {
        Vector rowData = (Vector) (tbData.elementAt(row));
        return rowData.elementAt(column);
    }

    @Override
    public void setValueAt(Object value,
            int row,
            int col) {
        Vector v = (Vector) tbData.get(row);
        v.set(col, value);
        fireTableCellUpdated(row, col);
    }

    public void insertRow(Vector v) {
        tbData.addElement(v);
        fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
    }

    public void deleteRow(int index) {
        tbData.removeElementAt(index);
        fireTableRowsDeleted(index, index);
    }
    
    public boolean isCellEditable(int row, int column){
        return false;
    }
}